CPU 686
BITS 32

%define DATA_REG 0x60
%define EOI 0x20
%define PIC1_CMD 0x20
%define PIC2_CMD 0xA0

global _mouse_handler
extern _arr
global _indice ;index of the last read byte

extern _mouse_queue ;pointer to the queue where the mouse information is stored 
extern _putGQueue
extern _event

section .data
_indice dd 0

section .text

_mouse_handler:
	push eax
	push ebx
	
	or dword[_event], 16
	
	xor eax, eax
	in al, DATA_REG
	mov ebx, dword[_indice]
	
	cmp ebx, 2
	je byte3 ;3 is the next byte to be read
	
	cmp ebx, 1
	je byte2;2 is the next byte to be read
	
byte1:
	mov byte[_arr], al
	mov ebx, 1
	jmp fim

byte2:
	mov byte[_arr + 1], al
	mov ebx, 2
	jmp fim

byte3:
	mov byte[_arr + 2], al
	
	push _arr
	push dword[_mouse_queue]
	call _putGQueue
	add esp, 8
	
	mov ebx, 3

fim:
	mov dword[_indice], ebx
	
	
	mov al, EOI
	out PIC1_CMD, al
	out PIC2_CMD, al

	pop ebx
	pop eax

	iretd
END
